/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.frame;

import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.PopupBox;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.util.frame.FrameHandler;
import org.mobiubiq.directory.client.Constants;
import org.mobiubiq.directory.client.data.Bookmark;
import org.mobiubiq.directory.client.data.BookmarksDataProvider;
import org.mobiubiq.directory.client.object.AbstractObject;

/**
 *
 * @author Marnix Renne
 */
public class Bookmarks extends DataProviderBaseFrame {
    private Bookmark selected;
    public Bookmarks() {
        super("bookmarks.xml", new BookmarksDataProvider());
        BookmarksDataProvider provider = (BookmarksDataProvider) getDataProvider();
        if(provider.countItemValues(BookmarksDataProvider.BOOKMARKS) > 0) {
            selected = (Bookmark) provider.getFirstItem(BookmarksDataProvider.BOOKMARKS);
        }
    }

    public boolean onMessage(Object identifier, Object[] arguments) {
        if (super.onMessage(identifier, arguments)) {
            BookmarksDataProvider dataProvider = (BookmarksDataProvider) getDataProvider();
            if ("add".equals(identifier)) {
                Kuix.getFrameHandler().pushFrame(new BookmarksEdit(dataProvider));
                return false;
            }
            if ("edit".equals(identifier)) {
                Kuix.getFrameHandler().pushFrame(new BookmarksEdit(dataProvider, selected));
                return false;
            }
            if ("deleteConfirm".equals(identifier)) {
                Kuix.alert(Kuix.getMessage("BOOKMARK_DELETE_CONFIRM"), KuixConstants.ALERT_OK | KuixConstants.ALERT_CANCEL, "delete", null, null, null);
                return false;
            }
            if ("delete".equals(identifier)) {
                try {
                    dataProvider.removeBookmark(selected);
                } catch (RecordStoreNotOpenException ex) {
                    Kuix.alert(ex);
                } catch (RecordStoreException ex) {
                    Kuix.alert(ex);
                }
                return false;
            }
            if("deleteAllConfirm".equals(identifier)) {
                Kuix.alert(Kuix.getMessage("BOOKMARK_DELETEALL_CONFIRM"), KuixConstants.ALERT_OK | KuixConstants.ALERT_CANCEL, "deleteAll", null, null, null);
                return false;
            }
            if ("deleteAll".equals(identifier)) {
                dataProvider.removeAll();
                return false;
            }
            if ("open".equals(identifier)) {
                ListItem item = (ListItem) arguments[0];
                Bookmark bookmark = (Bookmark) item.getDataProvider();
                AbstractObject object = bookmark.getObject();
                FrameHandler handler = Kuix.getFrameHandler();
                handler.pushFrame(new Services(object));
                return false;
            }
            if("select".equals(identifier)) {
                ListItem item = (ListItem) arguments[0];
                selected = (Bookmark) item.getDataProvider();
            }
        }
        return false;
    }

    public void cleanUp() {
        BookmarksDataProvider dataProvider = (BookmarksDataProvider) getDataProvider();
        dataProvider.close();
        super.cleanUp();
    }

    public void onAdded() {
        super.onAdded();
        BookmarksDataProvider dataProvider = (BookmarksDataProvider) getDataProvider();
        Desktop desktop = Kuix.getCanvas().getDesktop();
        Widget container = null;
        if(dataProvider.countItemValues(BookmarksDataProvider.BOOKMARKS) > 0) {
            container = desktop.getWidget("bookmarks");
        } else {
            container = desktop.getWidget("noBookmarksAvailable");
        }
        container.setVisible(true);
    }    
}
